"""
   ┌─┐       ┌─┐ + +
┌──┘ ┴───────┘ ┴──┐++
│                 │
│       ───       │++ + + +
███████───███████ │+
│                 │+
│       ─┴─       │
│                 │
└───┐         ┌───┘
    │         │
    │         │   + +
    │         │
    │         └──────────────┐
    │                        │
    │                        ├─┐
    │                        ┌─┘
    │                        │
    └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + +
      │ ─┤ ─┤       │ ─┤ ─┤
      └──┴──┘       └──┴──┘  + + + +
             神兽保佑
            代码无BUG!
"""
import os

import pandas as pd

from operating_sql import SQLManager


class Img_xlxs:
    def __init__(self):
        self.data_Frame = pd.DataFrame(columns=["id", "img_url", "content", "tags"])
        self.sql = SQLManager()

    def read_file(self):
        """
        :return: 返回所有Excel表中的数据
        """
        xlxs_data = []
        file_data = pd.read_excel("./file/new_weibo.xlsx")
        for data in file_data.values:
            xlxs_data.append(data)
        print(len(xlxs_data))
        return xlxs_data

    def read_img_id(self):
        img_id = []
        for img in os.listdir("./new_file"):
            id_ = img.split("_")[0]
            img_id.append(id_)
        print(len(img_id))
        return img_id

    def data_process(self):
        img_id = self.read_img_id()
        xlxs_data = self.read_file()
        for data in xlxs_data:
            if str(data[0]) in img_id:
                self.data_Frame = self.data_Frame.append(
                    pd.DataFrame(
                        {"id": [data[0]], "img_url": [data[1]], "content": [data[2]], "tags": [data[3]]})
                )
                print(data)
        self.data_Frame.to_excel("微博.xlsx", '微博内容', index=False)

    def ins_sql(self):
        file_data = pd.read_excel("./file/微博.xlsx")
        for data in file_data.values:
            ins_sql = 'insert into weibo(img_id,content,img_url,tags) values("%s","%s","%s","%s")' % (
                data[0], data[1], data[2], data[3])
            self.sql.run(ins_sql)

    def run(self):
        # self.ins_sql()
        self.data_process()


if __name__ == '__main__':
    img = Img_xlxs()
    img.run()
